package com.lanren.common.utils.anim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

/**
 * @ClassName HiddenAnimUtils
 * @Description TODO 伸缩动画
 * @Author chongheng.wang
 * @Date 2019/8/15 20:56
 * @Version 1.0
 */
public class HiddenAnimUtils {
    private int mHeight;//伸展高度

    private View hideView;//需要展开隐藏的布局，开关控件

    private HiddenAnimListener hiddenAnimListener;

    public interface HiddenAnimListener {
        void onEnd();
    }

    public void setHiddenAnimListener(HiddenAnimListener hiddenAnimListener) {
        this.hiddenAnimListener = hiddenAnimListener;
    }

    /**
     * 构造器(可根据自己需要修改传参)
     *
     * @param hideView 需要隐藏或显示的布局view
     */
    public static HiddenAnimUtils getInstance(View hideView) {
        return new HiddenAnimUtils(hideView);
    }

    /**
     * 构造器(可根据自己需要修改传参)
     *
     * @param context  上下文
     * @param hideView 需要隐藏或显示的布局view
     * @param height   布局展开的高度(根据实际需要传)
     */
    public static HiddenAnimUtils getInstance(Context context, View hideView, int height) {
        return new HiddenAnimUtils(context, hideView, height);
    }

    private HiddenAnimUtils(View hideView) {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        hideView.measure(w, h);

        this.hideView = hideView;
        mHeight = hideView.getMeasuredHeight();//伸展高度
    }

    private HiddenAnimUtils(Context context, View hideView, int height) {
        this.hideView = hideView;
        float mDensity = context.getResources().getDisplayMetrics().density;
        mHeight = (int) (mDensity * height + 0.5);//伸展高度
    }

    /**
     * 开关
     */
    public HiddenAnimUtils toggle(boolean isOpen) {
        if (isOpen) {
            closeAnimate(hideView);//布局隐藏
        } else {
            openAnim(hideView);//布局铺开
        }
        return this;
    }


    private void openAnim(final View v) {
        v.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(v, 0, mHeight);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (hiddenAnimListener != null) {
                    hiddenAnimListener.onEnd();
                }
            }
        });
        animator.start();
    }

    private void closeAnimate(final View view) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }

    private ValueAnimator createDropAnimator(final View v, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator arg0) {
                int value = (int) arg0.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }

}
